package com.zofer.study.linkedlist;

/**
 * 删除给出链表中的重复元素
 * 删除给出链表中的重复元素（链表中元素从小到大有序），使链表中的所有元素都只出现一次
 * 例如：
 * 给出的链表为1  > 1 > 2,返回1 > 2.
 * 给出的链表为1→1→2→3→3,返回1→2→3.
 */
public class DeleteDuplicates {

    public static void main(String[] args) {
        ListNode listNode3 = new ListNode(3);
        ListNode listNode2 = new ListNode(2);
        listNode2.next = listNode3;
        ListNode listNode1 = new ListNode(2);
        listNode1.next = listNode2;
        ListNode listNode = new ListNode(1);
        listNode.next = listNode1;
        ListNode node = deleteDuplicates(listNode);
        System.out.println("链表:"+node);
    }

    public static ListNode deleteDuplicates (ListNode head) {
        // write code here
        ListNode phead = head;
        while(phead != null){
            while(phead.next != null && phead.val == phead.next.val){
                phead.next = phead.next.next;
            }
            phead = phead.next;
        }
        return head;
    }
}
